<?php
	if(!defined('BASEPATH'))
		exit('No direct script access allowed');

	class m_userprofile extends CI_Model
	{
		var $userId = 0;
		var $firstName = '';
		var $lastName = '';
		var $address = '';
		var $city = '';
		var $state = '';
		var $zipCode = '';
		var $homePhone = '';
		
		function __construct()
    	{
			// Call the Model constructor
			parent::__construct();
			$this->load->database();
    	}

    	function profile_exist($user_id)
    	{
			$query = $this->db->get_where('tblUserProfile', array('userId' => $user_id));
			return (sizeof($query->row()) > 0);
    	}

    	function get_profile($user_id)
    	{
			$query = $this->db->get_where('tblUserProfile', array('userId' => $user_id));
			if ((sizeof($query->row()) > 0))
			{
				$this->userId = $user_id;
				$this->set_first_name($query->row()->firstName);
				$this->set_last_name($query->row()->lastName);
				$this->set_address($query->row()->address);
				$this->set_city($query->row()->city);
				$this->set_state($query->row()->state);
				$this->set_zip_code($query->row()->zipCode);
				$this->set_home_phone($query->row()->homePhone);
			}
			return $this;
    	}
    	
    	function set_user_id($id)
    	{
    		$this->userId = $id;
    	}
    	
    	function set_first_name($fn)
    	{
    		$this->firstName = $fn;
    	}
    	
    	function set_last_name($ln)
    	{
    		$this->lastName = $ln;
    	}
    	
    	function set_address($add)
    	{
    		$this->address = $add;
    	}
    	
    	function set_city($ci)
    	{
    		$this->city = $ci;
    	}
    	
    	function set_state($st)
    	{
    		$this->state = $st;
    	}
    	
    	function set_zip_code($zip)
    	{
    		$this->zipCode = $zip;
    	}
    	
    	function set_home_phone($num)
    	{
    		$this->homePhone = $num;
    	}

		function edit_user_profile()
		{
			if($this->profile_exist($this->userId))
			{
				$this->update_user_profile($this);
			}
			else
			{
				$this->insert_user_profile($this);
			}
		}

    	function insert_user_profile()
    	{
			$result = $this->db->insert('tblUserProfile', $this);
			return $result;
		}

		function update_user_profile()
		{
			$this->db->where('userId', $this->userId);
			$result = $this->db->update('tblUserProfile', $this);
			return $result;
		}
	}
?>
